package com.topofstack.flp.utils;

import java.io.File;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.media.ThumbnailUtils;
import android.provider.MediaStore.Video.Thumbnails;

public class VideoThumbnailAdapter extends BaseAdapter {
    /** The parent context */
    private Context myContext;
    private File[] videos;
    
    /** Simple Constructor saving the 'parent' context. */
    public VideoThumbnailAdapter(Context c, File[] files) {
    	this.myContext = c;
    	this.videos = files;
    }

    
    @Override
    public int getCount() {
    	return this.videos.length;
    }
    
    @Override
    public Object getItem(int position) { 
    	return this.videos[position]; 
    }
    
    @Override
    public long getItemId(int position) { 
    	return position; 
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(this.myContext);
        try {
        	 //Bitmap bmp = decodeSampledBitmapFromFile(this.videos[position].getAbsolutePath(), 100, 100);
        	 Bitmap bmp =  ThumbnailUtils.createVideoThumbnail(this.videos[position].getAbsolutePath(), 
         			Thumbnails.MICRO_KIND);
             i.setImageBitmap(bmp);
             
             //i.setImageURI(Uri.fromFile(this.images[position]));
             /* Image should be scaled as width/height are set. */
             i.setScaleType(ImageView.ScaleType.FIT_XY);
             /* Set the Width/Height of the ImageView. */
             i.setLayoutParams(new Gallery.LayoutParams(150, 150));
		} catch (Exception e) {
			e.printStackTrace();
		}
        return i;
    }
    
    
    /*
    public float getScale(boolean focused, int offset) {
    	return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
	}
	*/
    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
	    // Raw height and width of image
	    final int height = options.outHeight;
	    final int width = options.outWidth;
	    int inSampleSize = 1;
	
	    if (height > reqHeight || width > reqWidth) {
	        if (width > height) {
	            inSampleSize = Math.round((float)height / (float)reqHeight);
	        } else {
	            inSampleSize = Math.round((float)width / (float)reqWidth);
	        }
	    }
	    return inSampleSize;
    }
    
    
    public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inPurgeable = true;
        BitmapFactory.decodeFile(path, options);
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(path, options);
    }    
}